<?php

class Default_Model_Upgrade_StudSemeAbs extends Default_Model_Upgrade_Core
{

	protected $_table = 'stud_seme_abs';

	protected $_targetTable = array('student_semester');

	function upgrade (){
		if ($this->isUpgrade())
		return 'isUpgrde';
		else {
			$query = "SELECT * FROM stud_seme_abs ";
			$res = $this->_sfs3Pdo->query($query)->fetchAll();
			foreach($res as $row) {
				switch($row['abs_kind']){
					case '1' :
						 $kind = 'personal_leave';
					break;
					case '2' :
						 $kind = 'sick_leave';
					break;
					case '3' :
						 $kind = 'truancy';
					break;
					case '4' :
						 $kind = 'assembly';
					break;
					case '5' :
						 $kind = 'official_leave';
					break;
					case '6' :
						$kind = 'other_leave';
						break;
				}
				$set[$kind] = $row['abs_days'];
				$year = (int)substr($row['seme_year_seme'],0,-1);
				$semester = (int)substr($row['seme_year_seme'],-1);

				$where = " year=$year AND semester=$semester AND student_id='{$row['stud_id']}'";
				$this->newdb->update('student_semester',$set,$where);

			}
			$log['memo'] = '學生出缺席資料';
			$log['upgrade_time']	= date("Y-m-d h:i:s");

			$this->finishUpgrade($log);
		}
	}
}